package handels

import (
	"building-mgr/models"
	"github.com/gin-gonic/gin"
	"strconv"
)

func Add(c *gin.Context) {

	title := c.PostForm("title")
	comment := c.PostForm("comment")
	data := models.Video{

		Title:   title,
		Comment: comment,
	}

	models.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size

	var video []models.Video

	models.DB.Offset(offset).Limit(size).Find(&video)

	var total int64
	models.DB.Model(&models.Video{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "成功",
		"data":  video,
		"total": total,
	})
	return
}
